package org.edu.modules.stu.stu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.dto.BaseCountDto;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.common.entity.VeBaseAppUser;
import org.edu.modules.stu.common.service.VeBaseAppUserService;
import org.edu.modules.stu.common.service.VeBaseTeacherService;
import org.edu.modules.stu.stu.dto.VeStuPunishDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.stu.entity.VeStuPunish;
import org.edu.modules.stu.stu.service.VeStuPunishService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Api(tags = {"惩罚信息表"})
@RestController
@RequestMapping("/stu/veStuPunish")
@Slf4j
public class VeStuPunishController {

    @Autowired
    private VeStuPunishService veStuPunishService;

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @Autowired
    private VeBaseTeacherService veBaseTeacherService;

    @ApiOperation("分页查询惩罚信息表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeStuPunish> page) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        IPage<VeStuPunishDto> pageDto = null;
        QueryWrapper<VeStuPunish> queryWrapper = page.buildQueryWrapper(VeStuPunish.class);
        if (appUser.getUserType().contains("2")) {
            //如果用户类型是2，为学生账号
            pageDto = veStuPunishService.pageDtoByUserId(page, queryWrapper, appUser.getUserId());
            return Result.OK(pageDto);
        } else if (appUser.getUserType().contains("1")) {
            //如果用户类型是1，为教师账号
            List<Integer> studentIdsList = veBaseTeacherService.getStudentIdsListBySysUserId(appUser.getUserId());

            queryWrapper.in("d.stu_id", studentIdsList);
            pageDto = veStuPunishService.pageDto(page, queryWrapper);
            return Result.OK(pageDto);
        } else {
            pageDto = veStuPunishService.pageDto(page, queryWrapper);
        }
        return Result.OK(pageDto);
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeStuPunish> page) {
        QueryWrapper<VeStuPunish> queryWrapper = page.buildQueryWrapper(VeStuPunish.class);
        List<VeStuPunish> list = veStuPunishService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }


    /**
     * 查询所有
     */
    @ApiOperation("统计")
    @PostMapping("/countBy")
    public Result<?> countBy(@RequestBody AllDto<VeStuPunish> page) {
        QueryWrapper<VeStuPunish> queryWrapper = page.buildQueryWrapper(VeStuPunish.class);
        List<BaseCountDto> list = veStuPunishService.countBy(queryWrapper);
        return Result.OK(list);
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeStuPunish> page, HttpServletResponse resp) {
        QueryWrapper<VeStuPunish> queryWrapper = page.buildQueryWrapper(VeStuPunish.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("学生姓名", "name"));
        list.add(new ExcelPo("学生学号", "code"));
        list.add(new ExcelPo("班级", "bjText"));
        list.add(new ExcelPo("惩罚类型", "typeName"));
        list.add(new ExcelPo("惩罚时间", "punishTime"));
        list.add(new ExcelPo("惩罚描述", "content"));
        PoiUtils.downloadFile(resp, veStuPunishService.pageDto(page, queryWrapper).getRecords(), list);
        return Result.OK();
    }

    @AutoLog(value = "惩罚信息表-分页列表查询")
    @ApiOperation(value = "惩罚信息表-分页列表查询", notes = "惩罚信息表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeStuPunish entity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeStuPunish> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeStuPunish> page = new PageDto<VeStuPunish>(pageNo, pageSize);
        IPage<VeStuPunish> pageList = veStuPunishService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeStuPunish dto = veStuPunishService.findDtoById(id);
        if (dto == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStuPunishDto dto) {
        veStuPunishService.insertOrUpdate(dto, true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeStuPunishDto dto) {
        veStuPunishService.insertOrUpdate(dto, false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        veStuPunishService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeStuPunish> updateWrapper = new UpdateWrapper<>();
        if (statusDto.getIds() != null && statusDto.getIds().size() > 0) {
            updateWrapper.set("status", statusDto.getStatus());
            updateWrapper.in("id", statusDto.getIds());
            veStuPunishService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        } else {
            return Result.error("未选择!");
        }
    }


}
